/**
 * create by gby 20180925
 */
$(function () {
    obj.init();
});

var obj = new function () {
    'use strict';
    return {
        dataGrid: null,
        list: null,
        params: {
            currentId: null
        },
        init: function () {
            var $this = this;
            $('.xh-datagrid-toolbar').parseToolbar();
            this.dataGrid = $('#List');
            xhExtra.disableButton($("#bt_list_del"),'eu');
            xhExtra.disableButton($("#bt_list_edit"),'eu');
            this.dataGrid.treegrid({
                rownumbers: true,
                pagination: true,
                pageSize: 50,
                pageList: [20,50,100,200],
                onSelect: function (row) {
                    $this.params.currentId = row['id'];
                    $this.dataGrid.treegrid('expand', $this.params.currentId);
                    $('#bt_list_del').setButtonEnable('eu');
                    $('#bt_list_edit').setButtonEnable('eu');
                },
                onBeforeSelect: function (row) {
                    if ($this.params.currentId == undefined) {
                        return true;
                    } else {
                        if ($this.params.currentId == row['id']) {
                            $this.dataGrid.treegrid('unselect', $this.params.currentId);
                            $this.dataGrid.treegrid('collapse', $this.params.currentId);
                            $this.params.currentId = null;
                            $('#bt_list_del').setButtonDisable('eu');
                            $('#bt_list_edit').setButtonDisable('eu');
                            return false;
                        }
                    }
                },
                onBeforeCheck: function (row) {
                    if ($this.params.currentId == undefined) {
                        return true;
                    } else {
                        if ($this.params.currentId == row['id']) {
                            $this.dataGrid.treegrid('uncheckRow', $this.params.currentId);
                            $this.params.currentId = null;
                            $('#bt_list_del').setButtonDisable('eu');
                            $('#bt_list_edit').setButtonDisable('eu');
                            return false;
                        }
                    }
                }
            });
            this.listener();
        },
        listener: function () {
            var $this = this;
            //添加
            $('#bt_list_add').click(function () {
                $this.funObj().openAddForm();
            });
            //编辑
            $('#bt_list_edit').click(function () {
                if (xhExtra.buttonDisabled($(this))) {
                    return false;
                }
                $this.funObj().openEditForm();
            });
            //删除
            $("#bt_list_del").click(function () {
                if (xhExtra.buttonDisabled($(this))) {
                    return false;
                }
                $this.funObj().askDel();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                // 增加
                openAddForm:function () {
                    var obj=$this.dataGrid.treegrid('getSelections')[0];
                    if(obj != undefined){
                        window.top.xhTop.showModal({
                            title:'新增分类',
                            content: '<iframe src="'+baseUrl+'Track/addClassify?iParentId='+obj['id']+'"  style="width: 100%;;border-style: none;padding: 0px;height: 220px"  scrolling="yes"   ></iframe>',
                            button:'提交',
                            backClose: false
                        });
                    }else {
                        window.top.xhTop.showModal({
                            title:'新增分类',
                            content: '<iframe src="'+baseUrl+'Track/addClassify"  style="width: 100%;;border-style: none;padding: 0px;height: 220px"  scrolling="yes"   ></iframe>',
                            button:'提交',
                            backClose: false
                        });
                    }
                },

                // 删除
                askDel:function () {
                    var id = $this.dataGrid.treegrid('getSelections')[0]['id'];
                    xhExtra.askChangeStatus('Track/deleteClassify',{id:id},null,'你确定要删除这个分类吗?该分类下的子分类也将删除?',function (res) {
                        window.top.xhTop.getActiveFrame().obj.dataGrid.treegrid('reload');
                    })
                },

                // 编辑修改
                openEditForm:function () {
                    var id = $this.dataGrid.treegrid('getSelections')[0]['id'];
                    window.top.xhTop.showModal({
                        title:'编辑部门',
                        content: '<iframe src="'+baseUrl+'Track/addClassify?id='+id+'"  style="width: 100%;;border-style: none;padding: 0px;height: 220px"  scrolling="yes"   ></iframe>',
                        button:'提交',
                        backClose: false,
                    });
                },
            }
        }
    }
};